import { Component, OnInit, OnDestroy, Input } from '@angular/core';

@Component({
  selector: 'app-service-quote-lj',
  templateUrl: './service-quote-lj.component.html',
  styleUrls: ['./service-quote-lj.component.scss'],
})
export class ServiceQuoteLjComponent implements OnInit, OnDestroy {

  @Input() tagData: any;
  checkedService: any;
  paramName: any;
  totalPrice: any;
  checkArtificialService = false;
  constructor() { }

  ngOnInit() {
    // this.demoData();
    // 新配置优先
    if (this.tagData.data.artificialService && this.tagData.data.artificialService.optional !== 'true') {
      this.checkArtificialService = true; // optional 不是true 表示 必选
    }
    if (this.tagData.data.serviceTime && this.tagData.data.serviceTime.timeItem.length > 0  && this.tagData.paramName) {

      this.tagData.data.serviceTime.timeItem[0].checked = true;
      this.checkedService = this.tagData.data.serviceTime.timeItem[0];
      this.getTotalPrice();
    } else if (this.tagData.data.data && this.tagData.data.data.length > 0 && this.tagData.paramName) {
      this.paramName = this.tagData.paramName;
      localStorage.setItem(this.paramName, JSON.stringify(this.tagData.data));
    }
    localStorage.setItem("needRefresh", "true");
  }

  getTotalPrice(){
    if (this.checkedService.artificialServicePrice && this.tagData.data.artificialService) {
      this.tagData.data.artificialService.price = this.checkedService.artificialServicePrice;
    }
    if (this.tagData.data.artificialService && this.tagData.data.artificialService.price && this.checkArtificialService) {
      this.totalPrice = Number(this.checkedService.price)  + Number(this.tagData.data.artificialService.price) ;
      this.checkedService.quote_is_artificial_set = 1;
    } else {
      this.totalPrice = Number(this.checkedService.price) ;
      this.checkedService.quote_is_artificial_set = 0;
    }
    
    this.checkedService.totalPrice = this.totalPrice;
    this.paramName = this.tagData.paramName;
    // todo price
    localStorage.setItem(this.paramName, JSON.stringify(this.checkedService));
  }
  serviceTimeClick(item,items){
    if (item.checked) {
      return;
    } else {
      items.forEach(element => {
        if (element === item) {
          item.checked = !item.checked;
          this.checkedService = item;
          this.getTotalPrice();

        } else {
          element.checked = false;
        }
      });
    }
  }

  artificialService(event){
    console.log(event.detail.checked);
    this.checkArtificialService = event.detail.checked;
    this.getTotalPrice();
  }
  ngOnDestroy() {
    // 使用刷新标志位
    // localStorage.setItem("needRefresh", "true");
    localStorage.removeItem(this.paramName);
  }



  demoData() {
    this.tagData = {
      type: "tag",
      style: "tag.AppQuotationLJTagLogic",
      name: null,
      linearLayout: "body",
      data: {
        // 单个时间不可选
        data: [
          {
            formKey: "service_mode",
            value: "2"
          },
          {
            formKey: "quote_service_days",
            value: "90"
          },
          {
            formKey: "quote_price",
            value: "1000"
          }
        ],
        title: "深圳秋实邀请您开通转店快服务",

        serviceTime: {
          label: "服务时间",
          optional:"true",
          type: "1",
          // 时间可选
          timeItem: [
            {
              content:"1个月",
              price: "1000",
              artificialServicePrice:'500',
              type: "转店快",
              quote_is_artificial_set:"1",
              data: [
              {
                formKey: "service_mode",
                value: "2"
              },
              {
                formKey: "quote_service_days",
                value: "90"
              },
              {
                formKey: "quote_price",
                value: "1000"
              }
          ]},
          {
            content:"成交为止",
            price: "3000",
            type: "转店快",
            artificialServicePrice:'1000',
            quote_is_artificial_set:"1",
            data: [
              {
                formKey: "service_mode",
                value: "1"
              },
              {
                formKey: "quote_service_days",
                value: "365"
              },
              {
                formKey: "quote_price",
                value: "3000"
              }]
          },
          {
            content:"2个月",
            price: "3000",
            type: "转店快",
            artificialServicePrice:'800',
            quote_is_artificial_set:"1",
            data: [
              {
                formKey: "service_mode",
                value: "1"
              },
              {
                formKey: "quote_service_days",
                value: "365"
              },
              {
                formKey: "quote_price",
                value: "3000"
              }]
          }
        ]
        },
        artificialService: {
          label: "人工服务",
          price: "500",
          desc: "撮合成交，协助签约",
          optional:"true"
        },
        referees: {
          name: "深圳秋实",
          phone: "18888888",
          avatar: "http://",
          title: "深圳秋实邀请您开通转店快服务",
          label: "签约推荐人"
        },
        price: "1000",
        type: "转店快",
        content: "服务1个月",
        showServiceType:'出售快',
        contentList: ["AI算法精准推荐找店客户", "服务期限15天", "自由联系推荐客户"],
        serviceDesc: {
          title: "服务承诺",
          desc: "6个月没有交易成功，客户可申请退款或者延长服务半年。最终解释权归蜂喔所有"
        }
      },

      paramName: "quote_id"

    };
  }

}
